<?php
class Metrics extends CI_Controller {
		function __construct()
			{
				parent::__construct();
				$this->is_logged_in(); 
				$this->load->model('newmetrics_model');
				$this->load->model('metrics_model');
			}
		function is_logged_in() //check for session 
		{
			$is_logged_in = $this->session->userdata('is_logged_in');
			if(!isset($is_logged_in) || $is_logged_in != true)
			{
				echo 'You don\'t have permission to access this page. <a href="../login">Login</a>';	
				die();		
				//$this->load->view('login_form');
			}		
		}
		function general_status()
	{
		//load general statistic - 
		$this->load->view('status');
	}
		//prototype function
		function view_graph($servername,$counter)
	{
		if($counter == 1)
		{
			$processed_data= $this->metrics_model->get_single_counter($servername,$counter);
			foreach( array_keys($processed_data) as $cname)
			{
				$cname=$cname;
			}
			$this->display_single_data($cname,$processed_data[$cname]);
		}
		else
		{
			$processed_data1= $this->metrics_model->get_single_counter($servername,$counter);
			foreach( array_keys($processed_data1) as $cname1)
			{
				$cname1=$cname1;
			}
			$processed_data2= $this->metrics_model->get_single_counter($servername,$counter+1);
			foreach( array_keys($processed_data2) as $cname2)
			{
				$cname2=$cname2;
			}
			
			
			$this->display_double_data($cname1,$processed_data1[$cname1],$cname2,$processed_data2[$cname2]);
		}
	}


	function get_value($server_string)
	{	
		//processing string first IS_RACKcpu, mem, dio, net
		$servername = substr($server_string,0,strlen($server_string)-3 );
		$countername = substr($server_string,-3);
		$processed_data=$this->newmetrics_model->get_counter_value($servername);
		$cnames=array_keys($processed_data);
		switch ($countername)
		{	case "cpu":
				
				$this->display_single_data($cnames[0],$processed_data[$cnames[0]]);
			break;
			case "mem":
				$this->display_double_data($cnames[1],$processed_data[$cnames[1]],$cnames[2],$processed_data[$cnames[2]]);
			break;
			case "dio": //disk io
				$this->display_double_data($cnames[3],$processed_data[$cnames[3]],$cnames[4],$processed_data[$cnames[4]]);
			break;
			case "rio": //raid io
				$this->display_double_data($cnames[9],$processed_data[$cnames[9]],$cnames[10],$processed_data[$cnames[10]]);
			break;
			case "fne": //front-end inet
				$this->display_double_data($cnames[5],$processed_data[$cnames[5]],$cnames[6],$processed_data[$cnames[6]]);
			break;
			case "bne": //back-end inet
				$this->display_double_data($cnames[7],$processed_data[$cnames[7]],$cnames[8],$processed_data[$cnames[8]]);
			break;
			case "swa":
				//$this->display_switchboard(1);
				$this->display_switchboard();
			break;
			case "swb":
				$this->display_switchboard();
			break;
		}	
	}		
	function display_switchboard()
	{
		//$string_url = '/SwitchBoard/perf.aspx?key=packet.total&l=All+Packets';
		$this->load->view('viewswb.php');
	}
	function display_single_data($data_name,$data) //ONE series
	{
		$content['graph_name']=$data_name;
		$content['data']=$data;
		$this->load->view('view',$content);
	}
	function display_double_data($data_name1,$data1,$data_name2,$data2) //TWO series
	{
		$content['graph_name1']=$data_name1;
		$content['data1']=$data1;
		$content['graph_name2']=$data_name2;
		$content['data2']=$data2;
		$this->load->view('mview',$content);
	}		
	////---DEBUG FUNCTIONS
	function check_data($servername)
	{		
					
		$query=$this->db->query("select distinct counter_name from $servername where counter_id=1 limit 0,1");
		$row = $query->row();
		$counter_name=$row->counter_name;
		$content['server_name']=$servername;			
		$content['graph_name']=$counter_name;
		$query=$this->db->query("select timestamp, value from $servername where counter_id=1 order by timestamp desc");

$i=0;
		foreach ($query->result_array() as $row)
			{
				$reading[$i]=array(($row['timestamp'])*1000, $row['value']);
				$i++;	

			}
		//$arr = array($counter_name=>json_encode($reading));
                //print_r(json_encode($reading));
		//$arr[]=array('new_counter'=>json_encode($reading));
			//print_r($arr[1]);
		//echo sizeof($arr);
		$content['data']=json_encode($reading);
		print_r(json_encode($reading));
		$this->load->view('view',$content);

	}	

}
?>
